/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import path from 'path';

import { NewSigningOptions } from '../../../builder/inner-java-command-builder/new-signing-options.js';
import { ToolChainsConst } from '../../../const/sdk-const.js';
import { ProjectModel } from '../../../model/project/project-model.js';
import { ProjectBuildProfile } from '../../../options/build/project-build-profile.js';
import { SdkInfo } from '../../../sdk/sdk-info.js';

import { CommonSignCommandBuilder } from './common-sign-command-builder.js';
import { SignModel } from './sign-model.js';
import SigningConfigBuildOpt = ProjectBuildProfile.SigningConfigBuildOpt;
import MaterialBuildOpt = ProjectBuildProfile.MaterialBuildOpt;

/**
 * sdk 新签名工具hap-sign-tool的命令行生成器
 *
 * @since 2022/03/01
 */
export class HapSignCommandBuilder extends CommonSignCommandBuilder {
  private static SIGN_MODE = 'localSign';

  constructor(projectModel: ProjectModel, signingConfig: SigningConfigBuildOpt, sdkInfo: SdkInfo, signModel: SignModel, signingOptions: NewSigningOptions) {
    super(projectModel, signingConfig, sdkInfo, signModel, signingOptions);
  }

  getSignTool(): string {
    return path.resolve(this._sdkInfo.getSignDir(), ToolChainsConst.NEW_SIGN_TOOL);
  }

  /**
   * 获取build-profile中的签名配置,加载到signOptions中
   */
  initCommandParams(material: MaterialBuildOpt): void {
    (this._signingOptions as NewSigningOptions)
      .sign()
      .addMode(HapSignCommandBuilder.SIGN_MODE)
      .addKeyStoreFile(material.storeFile)
      .addKeyStorePwd(this.getKeyStorePwd())
      .addKeyAlias(material.keyAlias)
      .addKeyPwd(this.getKeyPwd())
      .addSignAlg(material.signAlg)
      .addProfileFile(material.profile)
      .addAppCertFile(material.certpath)
      .addInputFile(this._signModel.getInputFilePath())
      .addOutputFile(this._signModel.getOutPutFilePath());
  }
}
